

package com.mt.restaurant.controller.material;

import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.restaurant.dto.material.SupplierEditDto;
import com.mt.restaurant.entity.material.Supplier;
import com.mt.restaurant.service.material.SupplierService;
import com.mt.restaurant.service.sale.AddressService;


@Api(tags = "供应商")
@RestController
@RequestMapping("/api/restaurant/material/Supplier")
@CrossOrigin(allowCredentials = "true")
public class SupplierController {
    private static Logger logger = LoggerFactory.getLogger(SupplierController.class);


    @Autowired
    private SupplierService supplierService;
    @Autowired
    private AddressService addressService;

    /**
     * 根据分页参数查询供应商集合
     *
     * @param pageDTO 分页条件
     */
//    @PreAuthorize("hasAuthority('restaurant:material:Supplier:view')")
    @ApiOperation("分页查询供应商")
    @ApiPageParam
    @PostMapping("/findSuppliers")
    public PageResultDTO findSuppliers(@RequestBody PageDTO pageDTO) {
        return this.supplierService.findSuppliers(pageDTO);
    }

    /**
     * 根据ID查询指定的供应商
     *
     * @param supplierId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:Supplier:view')")
    @ApiOperation("根据id查询供应商")
    @ApiPageParam
    @PostMapping("/findSupplier")
    public Supplier findSupplier(@RequestParam Long supplierId) {
        return this.supplierService.findSupplier(supplierId);
    }

    /**
     * 根据ID查询指定的供应商(包含外键名称)
     *
     * @param supplierId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:Supplier:view')")
    @ApiOperation("根据ID查询指定的供应商(包含外键名称)")
    @PostMapping("/findSupplierForView")
    public Supplier findSupplierForView(@RequestParam Long supplierId) {
        return this.supplierService.findSupplierWithForeignName(supplierId);
    }

    /**
     * 根据ID查询指定的供应商(包含供应商和外键名称)
     *
     * @param supplierId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:Supplier:view')")
    @ApiOperation("根据ID查询指定的供应商(包含供应商和外键名称)")
    @PostMapping("/findSupplierForEdit")
    public SupplierEditDto findSupplierForEdit(@RequestParam Long supplierId) {
        SupplierEditDto supplierEditDto = new SupplierEditDto();
        supplierEditDto.setSupplier(this.supplierService.findSupplierWithForeignName(supplierId));

        this.prepareSupplierEditDto(supplierEditDto);

        return supplierEditDto;
    }

    /**
     * 根据ID查询指定的供应商(只提取ID 和 Name)
     *
     * @param supplierId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:Supplier:view')")
    @ApiOperation("根据ID查询指定的供应商(只提取ID 和 Name)")
    @PostMapping("/findSuppliersWithIdNameById")
    public Supplier findSuppliersWithIdNameById(@RequestParam Long supplierId) {
        return this.supplierService.findSuppliersWithIdNameById(supplierId);
    }

    /**
     * 根据名称查询供应商集合(只提取ID 和 Name)
     *
     * @param supplierName 名称
     */
    @PreAuthorize("hasAuthority('restaurant:material:Supplier:view')")
    @ApiOperation("根据名称查询供应商集合(只提取ID 和 Name)")
    @PostMapping("/findSuppliersWithIdNameByName")
    public List<Supplier> findSuppliersWithIdNameByName(@RequestParam String supplierName) {
        return this.supplierService.findSuppliersWithIdNameByName(supplierName);
    }


    /**
     * 创建新的供应商
     */
    @PreAuthorize("hasAuthority('restaurant:material:Supplier:add')")
    @ApiOperation("创建新的供应商")
    @PostMapping("/createSupplier")
    public SupplierEditDto createSupplier() {
        SupplierEditDto supplierEditDto = new SupplierEditDto();
        supplierEditDto.setSupplier(new Supplier());

        this.prepareSupplierEditDto(supplierEditDto);
        return supplierEditDto;
    }

    private void prepareSupplierEditDto(SupplierEditDto supplierEditDto) {
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        supplierEditDto.setContactAddressAddresss(this.addressService.findAllAddresssWithIdName());
    }

    /**
     * 新增保存供应商
     *
     * @param supplier 实体对象
     */
    @PreAuthorize("hasAuthority('restaurant:material:Supplier:add')")
    @ApiOperation("新增保存供应商")
    @PostMapping("/saveSupplier")
    public Supplier saveSupplier(@RequestBody Supplier supplier) {
        return this.supplierService.saveSupplier(supplier);
    }

    /**
     * 修改保存供应商
     *
     * @param supplier 实体对象
     */
    @PreAuthorize("hasAuthority('restaurant:material:Supplier:update')")
    @ApiOperation("修改保存供应商")
    @PostMapping("/updateSupplier")
    public Supplier updateSupplier(@RequestBody Supplier supplier) {
        return this.supplierService.updateSupplier(supplier);
    }

    /**
     * 根据ID删除供应商
     *
     * @param supplierId ID
     */
    @PreAuthorize("hasAuthority('restaurant:material:Supplier:remove')")
    @ApiOperation("根据ID删除供应商")
    @PostMapping("/deleteSupplier")
    public void deleteSupplier(@RequestParam Long supplierId) {
        this.supplierService.deleteSupplier(supplierId);
    }


}

